![]() |
Java Database Programming with JDBC
by Pratik Patel Coriolis, The Coriolis Group ISBN: 1576100561 Pub Date: 10/01/96 |
Previous | Table of Contents | Next |
This code requires some explanation. The weightx and weighty properties determine how the space in the respective direction is distributed. If either weight property is set to 0, the default, any extra space is distributed around the outside of the components in the corresponding direction. The components are also centered automatically in that direction. If either weight property is set to 1, any extra space is distributed within the spaces between components in the corresponding direction. Hence, in setting the weightx=1, we have told the GridBagLayout layout manager to position the components on each row so that extra space is added equally between the components on that row. However, the rows of components are vertically clumped together because the weighty property is set to 0.0. Later on, well change weighty to 1 so that the large TextArea (the OutputField) takes up extra space equal to all the components added before it. Take a look at Figure 4.3, shown at the end of the chapter, to see what I mean.
We also set the anchor property to tell the GridBagLayout to position the components on the center, relative to each other. The fill property is set to NONE so that the components are not stretched to fill empty space. You will find this technique to be useful when you want a large graphics area (Canvas) to take up any empty space that is available around it, respective to the other components. The gridwidth is set to REMAINDER to signal that any component assigned the GridBagContstraint Con takes up the rest of the space on a row. Similarly, we can set gridheight to REMAINDER so that a component assigned this constraint takes up the remaining vertical space. The last detail associated with GridBagLayout involves assigning the properties to the component. This is done via the setConstraints method in GridBagLayout.
Listing 4.4 shows how we do this. Notice that we assign properties for the TextArea, but not for the Labels. Because were positioning the Labels on the right side of the screen (the default), there is no need to assign constraints. There are more properties you can set with GridBagLayout, but its beyond the scope of this book.
Listing 4.4 Assigning properties to components.
add(new Label("Name")); gridbag.setConstraints(NameField, Con); add(NameField); // Note that we did not setConstraints for the Label. The GridbagLayout // manager assumes they carry the default constraints. The NameField is // assigned to be the last component on its row via the constraints Con, // then added to the user interface. add(new Label("Database URL")); gridbag.setConstraints(DBurl, Con); add(DBurl); gridbag.setConstraints(ConnectBtn, Con); add(ConnectBtn); // Here, we only want the ConnectBtn button on a row, by itself, so we // set the constraints, and add it. add(new Label("SQL Query")); gridbag.setConstraints(QueryField, Con); add(QueryField); Label result_label = new Label("Result"); result_label.setFont(new Font("Helvetica", Font.PLAIN, 16)); result_label.setForeground(Color.blue); gridbag.setConstraints(result_label, Con); add(result_label); // Here we add a label on its own line. We also set the colors for it. Con.weighty=1.0; gridbag.setConstraints(OutputField, Con); OutputField.setForeground(Color.white); OutputField.setBackground(Color.black); add(OutputField); // This is what we were talking about before. We want the large OutputField to // take up as much of the remaining space as possible, so we set the // weighty=1 at this point. This sets the field apart from the previously // added components, and gives it more room to exist in. show(); } //init
Everything has been added to the user interface, so lets show it! We also dont need to do anything else as far as preparation, so that ends the init method of our applet. Now we can move on to handling events.
We want to watch for four events when our applet is running: the user pressing the Enter key in the DBurl, NameField, and QueryField TextAreas, and the user clicking on the Connect button. Earlier in the chapter, we saw how to watch for events, but now we get to see what we do once the event is trapped, as shown in Listing 4.5. The event handling code is contained in the generic handleEvent method.
Listing 4.5 Handling events.
public boolean handleEvent(Event evt) { // The standard format for this method includes the Event class where // all the properties are set. if (evt.target == NameField) {char c=(char)evt.key; // Look for the Enter key pressed in the NameField. if (c == '\n') { Name=NameField.getText(); // Set the global Name variable to the contents in the NameField. return true; } else { return false; } } if (evt.target == DBurl) {char c=(char)evt.key; // Look for the enter key pressed in the DBurl TextArea. if (c == '\n') { url=DBurl.getText(); // Set the global url variable to the contents of the DBurl TextArea. return true; } else { return false; } } if (evt.target == QueryField) {char c=(char)evt.key; // Look for the Enter key pressed in the QueryField. if (c == '\n') { OutputField.setText(Select(QueryField.getText())); // Get the contents of the QueryField, and pass them to the Select // method that is defined in Listing 4.7. The Select method executes the // entered query, and returns the results. These results are shown in the // OutputField using the setText method. return true; } else { return false; } }
Previous | Table of Contents | Next |